package aeso.br.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;


import aeso.br.model.Categoria;
import aeso.br.util.HibernateUtil;

public class CategoriaDAO {
	
	public void add (Categoria categoria){
		 Session session = HibernateUtil.getSession().openSession();
	        Transaction t = session.beginTransaction();
	        session.save(categoria);
	        t.commit();
		
	}

	public Categoria getCategoria (int id){
		
		Session session = HibernateUtil.getSession().openSession();
		return (Categoria) session.load(Categoria.class, id);
		
		
	}
	
	public List<Categoria> listarCategorias(){
		Session session = HibernateUtil.getSession().openSession();
		Transaction t = session.beginTransaction();
		List lista  = session.createQuery("from categoria").list();
		t.commit();
		return lista;
		
	}
	
	public void remover(Categoria categoria){
		Session session = HibernateUtil.getSession().openSession();
		Transaction t = session.beginTransaction();
		session.delete(categoria);
		
	}
	
	public void alterar (Categoria categoria){
		Session session = HibernateUtil.getSession().openSession();
		Transaction t = session.beginTransaction();
		session.update(categoria);
		t.commit();
		
		
	}

}
